package com.hjx.controller;

import com.hjx.entity.SysRole;
import com.hjx.logging.LogAnnotation;
import com.hjx.response.AjaxResponse;
import com.hjx.response.ResponseType;
import com.hjx.service.SysRoleService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2020/6/16 - 16:16
 */
@RestController
@RequestMapping("role")
public class SysRoleController {
    @Resource
    SysRoleService sysRoleService;

    //根据RID查询单个角色
    @LogAnnotation
    @GetMapping("findOne/{rid}")
    public AjaxResponse findOne(@PathVariable String rid){
        SysRole role = sysRoleService.findOne(rid);
        if (role!=null){
            return new AjaxResponse(ResponseType.QUERY_SUCCESS,role);
        }
        return new AjaxResponse(ResponseType.QUERY_FAILED);
    }

    @LogAnnotation
    @GetMapping("findAll")
    public AjaxResponse findAll(){
        final List<SysRole> list = sysRoleService.findAll();
        return new AjaxResponse(ResponseType.QUERY_SUCCESS,list);
    }
    //查询全部角色以及对应权限
    @LogAnnotation
    @GetMapping("findAllRoleAndMenu")
    public AjaxResponse findAllRoleAndMenu(){
        final List<SysRole> list = sysRoleService.findAllRoleAndMenu();
        return new AjaxResponse(ResponseType.QUERY_SUCCESS,list);
    }

    @LogAnnotation
    @PostMapping("insert")
    public AjaxResponse insert(@RequestBody SysRole sysRole){
        boolean result = sysRoleService.insert(sysRole);
        if (result) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

    @LogAnnotation
    @PostMapping("insertBatch")
    public AjaxResponse insertBatch(@RequestBody SysRole[] roles){
        boolean result = sysRoleService.insertBatch(roles);
        if (result) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

    @LogAnnotation
    @PostMapping("update")
    public AjaxResponse update(@RequestBody SysRole sysRole){
        boolean result = sysRoleService.update(sysRole);
        if (result) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

    @LogAnnotation
    @GetMapping("delete/{rid}")
    public AjaxResponse delete(@PathVariable String rid){
        boolean result = sysRoleService.delete(rid);
        if (result) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }


}
